/**
 * Copyright By Nanjing Fujitsu Nanda Software Technology Co., Ltd
 * 下午6:28:13
 * DateUtils.java
 * 
 */
package com.fujitsu.cn.fnst.base;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

/**
 * @author qixb.fnst
 * 
 */
public class DateUtils {

	private static Calendar calendar = Calendar.getInstance();
	private static DateFormat dateTimeFormat = new SimpleDateFormat(
			"yyyy-MM-dd HH:mm:ss");
	private static DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");

	public static Date getBeginTimeOfDay(Date date) {
		calendar.setTime(date);
		calendar.set(Calendar.HOUR_OF_DAY, 0);
		calendar.set(Calendar.MINUTE, 0);
		calendar.set(Calendar.SECOND, 0);
		return calendar.getTime();
	}

	public static String formatTime(Date date) {
		if (date == null) {
			date = new Date();
		}
		return dateTimeFormat.format(date);
	}

	public static String format(Date date) {
		if (date == null) {
			date = new Date();
		}
		return dateFormat.format(date);
	}

	public static Date parseDate(String str) {
		Date date = null;
		try {
			date = dateFormat.parse(str);
		} catch (ParseException e) {
			date = new Date();
		}
		return date;
	}
	
	public static Date parseDateTime(String str) {
        Date date = null;
        try {
            date = dateTimeFormat.parse(str);
        } catch (ParseException e) {
            date = new Date();
        }
        return date;
    }

	public static Date getBeginTimeOfNextDay(Date date) {
		calendar.setTime(date);
		calendar.set(Calendar.HOUR_OF_DAY, 0);
		calendar.set(Calendar.MINUTE, 0);
		calendar.set(Calendar.SECOND, 0);
		calendar.set(Calendar.DATE, calendar.get(Calendar.DATE) + 1);
		return calendar.getTime();
	}

	public static Date getEndTime(Date openTime) {
		calendar.setTime(openTime);
		boolean result = calendar.get(Calendar.MINUTE) >= 30;
		calendar.add(Calendar.MINUTE, 30);
		calendar.set(Calendar.SECOND, 0);
		if (result) {

			calendar.set(Calendar.MINUTE, 0);
		} else {
			calendar.set(Calendar.MINUTE, 30);
		}
		return calendar.getTime();
	}

	public static Date getFormattedStartTime(Date startTime) {
		calendar.setTime(startTime);
		boolean result = calendar.get(Calendar.MINUTE) >= 30;
		calendar.set(Calendar.SECOND, 0);
		calendar.set(Calendar.MILLISECOND, 0);
		if (result) {
			calendar.set(Calendar.MINUTE, 30);
		} else {
			calendar.set(Calendar.MINUTE, 0);
		}
		return calendar.getTime();
	}

	public static Date getFormattedEndTime(Date endTime) {
		calendar.setTime(endTime);
		boolean result = calendar.get(Calendar.MINUTE) > 30;
		calendar.set(Calendar.SECOND, 0);
		calendar.set(Calendar.MILLISECOND, 0);
		if (result) {
			calendar.add(Calendar.HOUR, 1);
			calendar.set(Calendar.MINUTE, 0);
		} else {
			if (calendar.get(Calendar.MINUTE) != 0) {
				calendar.set(Calendar.MINUTE, 30);
			}
		}
		return calendar.getTime();
	}

	/**
	 * 获取之前没有被更新的时间
	 * */
	public static Date getBeginTime() {
		calendar.setTime(new Date());
		calendar.set(Calendar.MINUTE, 0);
		calendar.set(Calendar.HOUR, 0);
		calendar.set(Calendar.SECOND, 0);
		return calendar.getTime();
	}

	public static Date getEndTime() {
		return getEndTime(new Date());
	}

	public static Date getThreeDaysBeforeTime(Date now) {
		calendar.setTime(now);
		calendar.add(Calendar.DAY_OF_WEEK_IN_MONTH, -3);
		return calendar.getTime();
	}

	public static Date getThreeDaysAfterTime(Date now) {
		calendar.setTime(now);
		calendar.add(Calendar.DAY_OF_WEEK_IN_MONTH, 3);
		return calendar.getTime();
	}

	/**
	 * startDate 必须小于 endDate
	 * */
	public static List<Float> getFormattedDate(Date startDate, Date endDate) {
		List<Float> list = new ArrayList<Float>(8);
		startDate = getFormattedStartTime(startDate);
		endDate = getFormattedEndTime(endDate);
		@SuppressWarnings("deprecation")
		float begin = startDate.getHours() + startDate.getMinutes() / 60.0f;
		for (int i = 0; i < (endDate.getTime() - startDate.getTime()) / 1000 / 60 / 30; i++) {
			list.add(begin);
			begin += 0.5f;
		}
		return list;

	}

	/***
	 * @param date
	 *            时间
	 * @param weeks
	 *            距时间几周之后 <font color='red'>0</font>表示 <font
	 *            color='red'>当前周</font>
	 * 
	 *            获取距离data几周之后的周一-周日的日期
	 * **/
	public static List<Date> getWeekDate(Date date, int weeks) {
		calendar.setTime(date);
		int week = calendar.get(Calendar.DAY_OF_WEEK);
		if (week >= 2) {
			// 周一到周六
			calendar.add(Calendar.DATE, 7 * weeks - week + 1);
		} else {
			// 周日
			calendar.add(Calendar.DATE, 7 * weeks);
		}
		List<Date> dates = new ArrayList<Date>();
		for (int i = 0; i < 7; i++) {
			dates.add(calendar.getTime());
			calendar.add(Calendar.DATE, 1);
		}
		return dates;
	}

	public static Date getMonthBeginDay(Date date) {
		calendar.setTime(date);
		calendar.set(Calendar.DAY_OF_MONTH, 1);
		calendar.set(Calendar.HOUR, 0);
		calendar.set(Calendar.MINUTE, 0);
		calendar.set(Calendar.SECOND, 0);
		return calendar.getTime();
	}

	public static Date getMonthEndDay(Date date) {
		calendar.setTime(date);
		calendar.add(Calendar.MONTH, 1);
		calendar.set(Calendar.DAY_OF_MONTH, 1);
		calendar.set(Calendar.HOUR, 0);
		calendar.set(Calendar.MINUTE, 0);
		calendar.set(Calendar.SECOND, 0);
		calendar.add(Calendar.DAY_OF_MONTH, -1);
		return calendar.getTime();
	}

	public static Date add(Date date, int field, int amount) {
		calendar.setTime(date);
		calendar.add(field, amount);
		return calendar.getTime();
	}

	public static Date add(Date date, float amount) {
		calendar.setTime(date);
		calendar.set(Calendar.HOUR, 0);
		calendar.set(Calendar.MINUTE, 0);
		calendar.set(Calendar.SECOND, 0);
		calendar.add(Calendar.HOUR, (int) amount);
		if (amount != (int) amount) {
			calendar.add(Calendar.MINUTE, 30);
		}
		return calendar.getTime();
	}

	@SuppressWarnings("deprecation")
	public static Float getNow(Date date) {
		float result = 0.0f;
		int hour = date.getHours();
		if (date.getMinutes() > 30) {
			result = hour + 1;
		} else if (date.getMinutes() > 0) {
			result = hour + .5f;
		} else {
			result = hour;
		}
		return result;
	}
}
